package util.designpattern.proxyPattern;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 创建切换数据源的代理类
 */
public class OrderServicStaticProxy implements IOrderService {

    private SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy");
    private IOrderService orderService;

    public OrderServicStaticProxy(IOrderService orderService){
        this.orderService = orderService;
    }

    @Override
    public int createOrder(Order order) {
        before();
        Long time = order.getCreateTime();
        Integer dbRouter = Integer.valueOf(yearFormat.format((new Date(time))));
        System.out.println("静态代理类自动分配到【DB_" + dbRouter +"】数据源处理数据" );
        DynamicDataSourceEntry.set(dbRouter);
        orderService.createOrder(order);
        after();
        return 0;
    }

    private void before(){
        System.out.println("代理前处理的方法");
    }

    private void after(){
        System.out.println("代理后处理的方法");
    }
}
